<template>
	<view class="page_bg_FAFAFA">
		<view class="cr-333 font_s32 font_w600 padd-left53 mar-top54 line-height100">
			标题（非必填）
		</view>
		<view>
			<input v-model="form.title" placeholder="请输入您要上传的标题" placeholder-style="color:#999;font-size:30rpx;"
				class="input-box" />
		</view>
		<view class="cr-333 font_s32 font_w600 padd-left53 mar-top44 line-height100">
			内容
		</view>
		<view class="padd-top21">
			<textarea v-model="form.contentText" class="txtarea" placeholder="请输入您要上传的内容"
				placeholder-style="color:999;font-size:30rpx;"></textarea>
		</view>
		<view class="cr-333 font_s32 font_w600 padd-left53 mar-top28 line-height100">
			图片/视频
		</view>
		<view class="padd-left50 mar-top31 flex flex-wrap">
			<view v-for="(item,index) in filesArr" :key="index" class="flex align-center justify-center upbox">
				<image :src="item.thumbTempFilePath || item.tempFilePath" style="width: 100%;height: 100%;"></image>
				<view @click="delFile(index)" class="del-box">
					<u-icon name="close-circle-fill" color="red" size="20"></u-icon>
				</view>
			</view>
			<view @click="clickFlieAdd" class="flex align-center justify-center upbox">
				<image src="https://www.worldintek.com:39011/yaolexue-service/icon-upload.png" class="w-29 h-27"></image>
			</view>
		</view>
		<view class="h-200"></view>
		<view @click="clickSubmit" class="btn">
			提交
		</view>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				form: {
					title: '',
					contentText: ''
				},
				imageDtos: [],
				filesArr: []
			};
		},
		methods: {
			clickFlieAdd() {
				wx.chooseMedia({
					count: 9,
					mediaType: ['mix'],
					sourceType: ['album', 'camera'],
					maxDuration: 30,
					camera: 'back',
					success: (res) => {
						this.filesArr = [...this.filesArr, ...res.tempFiles]
						console.log(res)
					}
				})
			},
			delFile(index){
				this.filesArr.splice(index,1)
			},
			clickSubmit(){
				if(!this.form.contentText){
					uni.showToast({
						title:'请填写内容',
						icon:'none'
					})
					return
				}
				if(!this.filesArr){
					uni.showToast({
						title:'请选择图片/视频',
						icon:'none'
					})
					return
				}
				uni.showLoading({
					title:'上传中'
				})
				Promise.all(this.filesArr.map(item => this.uploadFile(item))).then(fileRep => {
					console.log(fileRep,'==>fileRep')
					uni.hideLoading()
					this.$http.post('/yaolexue-service-portal/contentItemMaterial/contentItemMaterialReport',{
						imageDtos:fileRep,
						...this.form						
					}).then(res => {
						if(res.code === 200){
							uni.showToast({
								title:'提交成功'
							})
						}else{
							uni.showToast({
								title:res.message,
								icon:'none'
							})
						}
					})
				})
			},
			uploadFile(file){
				return new Promise((resolve,reject) => {
					uni.uploadFile({
						url: `${this.$baseUrl}/yaolexue-service-portal/minio/uploadImg`, //仅为示例，非真实的接口地址
						filePath: file.tempFilePath,
						name: 'file',
						header:{
							Authorization:`Bearer ${uni.getStorageSync('token')}`
						},
						success: (res) => {
							if (res.statusCode === 200) {
								let json = JSON.parse(res.data);
								console.log(json,'==>json')
								if(json.code == 200){
									return resolve({
										...json.data,
										type:file.fileType == "video" ? 2 : 1
									});
								}								
							}
							reject();
						},
						fail: (e) => {
							reject('fail')
						}
					});
				})
			}
		}
	}
</script>

<style lang="scss" scoped>
	.txtarea {
		width: 650rpx;
		height: 329rpx;
		background: #F3F3F3;
		border-radius: 20rpx;
		padding: 31rpx 26rpx;
		margin: auto;
		font-size: 30rpx;
		font-family: Source Han Sans CN;
		font-weight: 400;
		color: #333;
	}

	.upbox {
		width: 153rpx;
		height: 153rpx;
		background: #F3F3F3;
		border-radius: 20rpx;
		margin: 10rpx 8rpx 0 0;
		position: relative;
		margin-right: 20rpx;
		.del-box{
			position: absolute;
			right: -10rpx;
			top: -10rpx;
		}
	}

	.btn {
		width: 561rpx;
		height: 86rpx;
		background: #FA312A;
		border-radius: 43rpx;
		bottom: 150rpx;
		font-size: 32rpx;
		color: #FFFFFF;
		display: flex;
		align-items: center;
		justify-content: center;
		margin-top: 100rpx;
		position: relative;
		left: 50%;
		transform: translateX(-50%);
	}

	.input-box {
		width: 650rpx;
		height: 89rpx;
		background: #F3F3F3;
		border-radius: 20rpx;
		padding: 0 24rpx;
		font-size: 30rpx;
		margin: 24rpx auto 0 auto;
	}
</style>